package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.SedeDAO;
import com.cs.beneficios.business.entity.SubProgramaDAO;
//import com.cs.beneficios.business.model.BeneficiarioVO;
//import com.cs.beneficios.business.model.BeneficioVO;
import com.cs.beneficios.business.model.SubProgramaVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;
import java.sql.SQLException;
import java.util.Collection;
//import java.util.LinkedList;
//import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class ValidarRenovarAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    HttpSession session = request.getSession();
    UserVO user = (UserVO)session.getAttribute("userInfo");
    String codeBeneficio = request.getParameter("codBeneficio");
    if((Object)codeBeneficio != null)
    {
      session.setAttribute("codBeneficio",codeBeneficio);  
    }else
    {
      codeBeneficio = (String)session.getAttribute("codBeneficio");
    }
    
    try
    {
      SubProgramaDAO subproDAO = new SubProgramaDAO(cf);
      SubProgramaVO subproVO = new SubProgramaVO();
      subproVO.setCode(new Long(codeBeneficio));
      Collection subpro = subproDAO.findSubProgramaByCriterea(subproVO,user.getCode(), user.getUserGroupName(),user.getCodeArea());
      cf.closeConnection(subproDAO.getConn());
      if(subpro != null && subpro.size() > 0)
      {
        try
        {
          SedeDAO objSedeDAO = new SedeDAO(cf);
          Collection sedes = objSedeDAO.GetTotalSedebyUser(user.getCode(),new Long(codeBeneficio),user.getUserGroupName(),user.getCodeArea());
          cf.closeConnection(objSedeDAO.getConn());
          if((Object)sedes != null)
          {
            if(sedes.size() > 0){
              session.setAttribute("sedes",sedes);
            }else{
                session.setAttribute("msg","No posee permisos para renovar el beneficio en ninguna sede.");
              return mapping.findForward("fail");
            }
          }else{
            session.setAttribute("msg","No posee permisos para renovar el beneficio en ninguna sede.");
            return mapping.findForward("fail");
          }          
        }catch(SQLException e)
        {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
        
      }else{
        session.setAttribute("msg","El beneficio no se encuentra activo por lo tanto no se puede renovar.");
        return mapping.findForward("fail");
      }
    }catch(SQLException e)
    {
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSSPE - cwsub ********/");
    }
    
    
    return mapping.findForward("success");
  }
}